package com.kcht.sylyg08.service;

import com.kcht.adp.common.log.Logger;
import com.kcht.adp.pagination.Page;
import com.kcht.sylyg08.dao.IReaderDao;
import com.kcth.sylyg08.entity.Reader;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Objects;

@RequestScoped
@Transactional()
public class ReaderService {
    @Inject
    private Logger logger;
    @EJB
    private IReaderDao dao;

    public void save(Reader reader) {
        if (reader.getId() == null) {
            dao.save(reader);
        } else {
            dao.merge(reader);
        }
    }

    public Page<Reader, Reader> list(Page<Reader, Reader> page) {
        return dao.getPageListByJPQL("select a from Reader a", page);
    }

    public void delete(List<Integer> ids) {
        ids.forEach(id -> {
            Reader reader = dao.get(id.longValue());
            if (Objects.nonNull(reader)) {
                dao.delete(id.longValue());
            }
        });
    }


}
